//
//  ZYCSettingLanguageViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/20.
//

#import "ZYCSettingLanguageViewController.h"
#import "ZYCSettingLanguageModel.h"
#import "ZYCMainViewController.h"
#import "ZYCNavigationController.h"
@interface ZYCSettingLanguageViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) UITableView *tableView;
@end

static NSString *const cellId = @"cellId";

@implementation ZYCSettingLanguageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = NSLocalizedString(@"语言设置", nil);
    
//    if (MQLanguageConfig.userLanguage.length > 0) {
//        NSString *str = [NSBundle currentLanguage];
//        for (NSInteger i = 0; i < self.dataArr.count; i++) {
//            ZYCSettingLanguageModel *model = self.dataArr[i];
//            model.select = [model.language isEqualToString:str];
//        }
//    }
    self.tableView.frame = CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight - kNavBarAndStatusBarHeight);
    [self.view addSubview:self.tableView];
}

#pragma mark - delegate ---> tableView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
    ZYCSettingLanguageModel *model = self.dataArr[indexPath.row];
    cell.textLabel.text = model.title;
    cell.accessoryType = (model.select) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    return cell;
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    ZYCSettingLanguageModel *model = self.dataArr[indexPath.row];
//    if (model.select) {
//        return;
//    }
//    for (ZYCSettingLanguageModel *model in self.dataArr) {
//        model.select = NO;
//    }
//    model.select = !model.select;
//    [tableView reloadData];
//    
////    MQLanguageConfig.userLanguage = model.language;
//    
//    
//    //创建新的UITabbarController
//    ZYCMainViewController *tabbar = [[ZYCMainViewController alloc] init];
//    //找到对应的nav
//    tabbar.selectedIndex = 4;
//    ZYCNavigationController *nav = tabbar.selectedViewController;
//    NSMutableArray *navVCs = nav.viewControllers.mutableCopy;
//    
//    //添加指定vc到nav栈中
//    ZYCSettingLanguageViewController *vc = [ZYCSettingLanguageViewController new];
//    vc.hidesBottomBarWhenPushed = YES;
//    [navVCs addObject:vc];
//    
//    //主线程刷新UI
//    dispatch_async(dispatch_get_main_queue(), ^{
//        [UIApplication sharedApplication].delegate.window.rootViewController = tabbar;
//        nav.viewControllers = navVCs;
//        NSLog(@"当前语言 %@", [NSBundle currentLanguage]);
//    });
//}



- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [UITableView new];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellId];
    }
    return _tableView;
}

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
        {
            ZYCSettingLanguageModel *model = [ZYCSettingLanguageModel new];
            model.title = NSLocalizedString(@"简体中文", nil);
            model.language = @"zh-Hans";
            [_dataArr addObject:model];
        }
        {
            ZYCSettingLanguageModel *model = [ZYCSettingLanguageModel new];
            model.title = NSLocalizedString(@"English", nil);
            model.language = @"en";
            [_dataArr addObject:model];
        }
    }
    return _dataArr;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
